package com.em.tuppence.web.security.details;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.em.tuppence.core.model.application.User;

public class TuppenceUserDetails implements UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//authorities
	private Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
	
	private User embeddedUser = null;
	
	public TuppenceUserDetails(User user) {
		this.embeddedUser = user;
		
		if(user != null) {
			//add authorities
			this.authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
		}
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if(this.embeddedUser == null) {
			return Collections.emptySet();
		}
		
		return this.authorities;
	}

	@Override
	public String getPassword() {
		return UUID.randomUUID().toString();
	}

	@Override
	public String getUsername() {
		return embeddedUser == null ? "anonymous" : this.embeddedUser.getOpenIdUrl();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

}
